home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gscolor1.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  8.0 KB  |  258 lines

  1. /* Copyright (C) 1989, 1992, 1993, 1994, 1996, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gscolor1.c,v 1.3 2000/09/19 19:00:26 lpd Exp $ */
  20. /* Level 1 extended color operators for Ghostscript library */
  21. #include "gx.h"
  22. #include "gserrors.h"
  23. #include "gsstruct.h"
  24. #include "gsutil.h"        /* for gs_next_ids */
  25. #include "gsccolor.h"
  26. #include "gscssub.h"
  27. #include "gxcspace.h"
  28. #include "gxdcconv.h"
  29. #include "gxdevice.h"        /* for gx_color_index */
  30. #include "gxcmap.h"
  31. #include "gzstate.h"
  32. #include "gscolor1.h"
  33.  
  34. /* Imports from gscolor.c */
  35. void load_transfer_map(P3(gs_state *, gx_transfer_map *, floatp));
  36.  
  37. /* Imported from gsht.c */
  38. void gx_set_effective_transfer(P1(gs_state *));
  39.  
  40. /* Force a parameter into the range [0.0..1.0]. */
  41. #define FORCE_UNIT(p) (p < 0.0 ? 0.0 : p > 1.0 ? 1.0 : p)
  42.  
  43. /* setcmykcolor */
  44. int
  45. gs_setcmykcolor(gs_state * pgs, floatp c, floatp m, floatp y, floatp k)
  46. {
  47.     gs_client_color *pcc = pgs->ccolor;
  48.  
  49.     if (pgs->in_cachedevice)
  50.     return_error(gs_error_undefined);
  51.     cs_adjust_color_count(pgs, -1);
  52.     gs_cspace_assign(pgs->color_space, gs_current_DeviceCMYK_space(pgs));
  53.     pgs->orig_cspace_index = pgs->orig_base_cspace_index =
  54.     gs_color_space_index_DeviceCMYK;
  55.     pcc->paint.values[0] = FORCE_UNIT(c);
  56.     pcc->paint.values[1] = FORCE_UNIT(m);
  57.     pcc->paint.values[2] = FORCE_UNIT(y);
  58.     pcc->paint.values[3] = FORCE_UNIT(k);
  59.     pcc->pattern = 0;        /* for GC */
  60.     gx_unset_dev_color(pgs);
  61.     return 0;
  62. }
  63.  
  64. /* currentcmykcolor */
  65. int
  66. gs_currentcmykcolor(const gs_state * pgs, float pr4[4])
  67. {
  68.     const gs_client_color *pcc = pgs->ccolor;
  69.     const gs_color_space *pcs = pgs->color_space;
  70.     const gs_color_space *pbcs = pcs;
  71.     const gs_imager_state *const pis = (const gs_imager_state *)pgs;
  72.     gs_color_space_index csi = pgs->orig_cspace_index;
  73.     frac fcc[4];
  74.     gs_client_color cc;
  75.     int code;
  76.  
  77.   sw:switch (csi) {
  78.     case gs_color_space_index_DeviceGray:
  79.         pr4[0] = pr4[1] = pr4[2] = 0.0;
  80.         pr4[3] = 1.0 - pcc->paint.values[0];
  81.         return 0;
  82.     case gs_color_space_index_DeviceRGB:
  83.         color_rgb_to_cmyk(float2frac(pcc->paint.values[0]),
  84.                   float2frac(pcc->paint.values[1]),
  85.                   float2frac(pcc->paint.values[2]),
  86.                   pis, fcc);
  87.         pr4[0] = frac2float(fcc[0]);
  88.         pr4[1] = frac2float(fcc[1]);
  89.         pr4[2] = frac2float(fcc[2]);
  90.         pr4[3] = frac2float(fcc[3]);
  91.         return 0;
  92.     case gs_color_space_index_DeviceCMYK:
  93.         pr4[0] = pcc->paint.values[0];
  94.         pr4[1] = pcc->paint.values[1];
  95.         pr4[2] = pcc->paint.values[2];
  96.         pr4[3] = pcc->paint.values[3];
  97.         return 0;
  98.     case gs_color_space_index_DeviceN:
  99.     case gs_color_space_index_Separation:
  100.       ds:if (cs_concrete_space(pbcs, pis) == pbcs)
  101.         break;        /* not using alternative space */
  102.         /* (falls through) */
  103.     case gs_color_space_index_Indexed:
  104.         pbcs = gs_cspace_base_space(pbcs);
  105.         switch (pbcs->type->index) {
  106.         case gs_color_space_index_DeviceN:
  107.         case gs_color_space_index_Separation:
  108.             goto ds;
  109.         default:    /* outer switch will catch undefined cases */
  110.             break;
  111.         }
  112.         code = cs_concretize_color(pcc, pcs, fcc, pis);
  113.         if (code < 0)
  114.         return code;
  115.         cc.paint.values[0] = frac2float(fcc[0]);
  116.         cc.paint.values[1] = frac2float(fcc[1]);
  117.         cc.paint.values[2] = frac2float(fcc[2]);
  118.         cc.paint.values[3] = frac2float(fcc[3]);
  119.         pcc = &cc;
  120.         pcs = pbcs;
  121.         csi = pgs->orig_base_cspace_index;
  122.         goto sw;
  123.     default:
  124.         break;
  125.     }
  126.     pr4[0] = pr4[1] = pr4[2] = 0.0;
  127.     pr4[3] = 1.0;
  128.     return 0;
  129. }
  130.  
  131. /* setblackgeneration */
  132. /* Remap=0 is used by the interpreter. */
  133. int
  134. gs_setblackgeneration(gs_state * pgs, gs_mapping_proc proc)
  135. {
  136.     return gs_setblackgeneration_remap(pgs, proc, true);
  137. }
  138. int
  139. gs_setblackgeneration_remap(gs_state * pgs, gs_mapping_proc proc, bool remap)
  140. {
  141.     rc_unshare_struct(pgs->black_generation, gx_transfer_map,
  142.               &st_transfer_map, pgs->memory,
  143.               return_error(gs_error_VMerror),
  144.               "gs_setblackgeneration");
  145.     pgs->black_generation->proc = proc;
  146.     pgs->black_generation->id = gs_next_ids(1);
  147.     if (remap) {
  148.     load_transfer_map(pgs, pgs->black_generation, 0.0);
  149.     gx_unset_dev_color(pgs);
  150.     }
  151.     return 0;
  152. }
  153.  
  154. /* currentblackgeneration */
  155. gs_mapping_proc
  156. gs_currentblackgeneration(const gs_state * pgs)
  157. {
  158.     return pgs->black_generation->proc;
  159. }
  160.  
  161. /* setundercolorremoval */
  162. /* Remap=0 is used by the interpreter. */
  163. int
  164. gs_setundercolorremoval(gs_state * pgs, gs_mapping_proc proc)
  165. {
  166.     return gs_setundercolorremoval_remap(pgs, proc, true);
  167. }
  168. int
  169. gs_setundercolorremoval_remap(gs_state * pgs, gs_mapping_proc proc, bool remap)
  170. {
  171.     rc_unshare_struct(pgs->undercolor_removal, gx_transfer_map,
  172.               &st_transfer_map, pgs->memory,
  173.               return_error(gs_error_VMerror),
  174.               "gs_setundercolorremoval");
  175.     pgs->undercolor_removal->proc = proc;
  176.     pgs->undercolor_removal->id = gs_next_ids(1);
  177.     if (remap) {
  178.     load_transfer_map(pgs, pgs->undercolor_removal, -1.0);
  179.     gx_unset_dev_color(pgs);
  180.     }
  181.     return 0;
  182. }
  183.  
  184. /* currentundercolorremoval */
  185. gs_mapping_proc
  186. gs_currentundercolorremoval(const gs_state * pgs)
  187. {
  188.     return pgs->undercolor_removal->proc;
  189. }
  190.  
  191. /* setcolortransfer */
  192. /* Remap=0 is used by the interpreter. */
  193. int
  194. gs_setcolortransfer_remap(gs_state * pgs, gs_mapping_proc red_proc,
  195.               gs_mapping_proc green_proc,
  196.               gs_mapping_proc blue_proc,
  197.               gs_mapping_proc gray_proc, bool remap)
  198. {
  199.     gx_transfer_colored *ptran = &pgs->set_transfer.colored;
  200.     gx_transfer_colored old;
  201.     gs_id new_ids = gs_next_ids(4);
  202.  
  203.     old = *ptran;
  204.     rc_unshare_struct(ptran->gray, gx_transfer_map, &st_transfer_map,
  205.               pgs->memory, goto fgray, "gs_setcolortransfer");
  206.     rc_unshare_struct(ptran->red, gx_transfer_map, &st_transfer_map,
  207.               pgs->memory, goto fred, "gs_setcolortransfer");
  208.     rc_unshare_struct(ptran->green, gx_transfer_map, &st_transfer_map,
  209.               pgs->memory, goto fgreen, "gs_setcolortransfer");
  210.     rc_unshare_struct(ptran->blue, gx_transfer_map, &st_transfer_map,
  211.               pgs->memory, goto fblue, "gs_setcolortransfer");
  212.     ptran->gray->proc = gray_proc;
  213.     ptran->gray->id = new_ids;
  214.     ptran->red->proc = red_proc;
  215.     ptran->red->id = new_ids + 1;
  216.     ptran->green->proc = green_proc;
  217.     ptran->green->id = new_ids + 2;
  218.     ptran->blue->proc = blue_proc;
  219.     ptran->blue->id = new_ids + 3;
  220.     if (remap) {
  221.     load_transfer_map(pgs, ptran->red, 0.0);
  222.     load_transfer_map(pgs, ptran->green, 0.0);
  223.     load_transfer_map(pgs, ptran->blue, 0.0);
  224.     load_transfer_map(pgs, ptran->gray, 0.0);
  225.     gx_set_effective_transfer(pgs);
  226.     gx_unset_dev_color(pgs);
  227.     }
  228.     return 0;
  229.   fblue:
  230.     rc_assign(ptran->green, old.green, "setcolortransfer");
  231.   fgreen:
  232.     rc_assign(ptran->red, old.red, "setcolortransfer");
  233.   fred:
  234.     rc_assign(ptran->gray, old.gray, "setcolortransfer");
  235.   fgray:
  236.     return_error(gs_error_VMerror);
  237. }
  238. int
  239. gs_setcolortransfer(gs_state * pgs, gs_mapping_proc red_proc,
  240.             gs_mapping_proc green_proc, gs_mapping_proc blue_proc,
  241.             gs_mapping_proc gray_proc)
  242. {
  243.     return gs_setcolortransfer_remap(pgs, red_proc, green_proc,
  244.                      blue_proc, gray_proc, true);
  245. }
  246.  
  247. /* currentcolortransfer */
  248. void
  249. gs_currentcolortransfer(const gs_state * pgs, gs_mapping_proc procs[4])
  250. {
  251.     const gx_transfer_colored *ptran = &pgs->set_transfer.colored;
  252.  
  253.     procs[0] = ptran->red->proc;
  254.     procs[1] = ptran->green->proc;
  255.     procs[2] = ptran->blue->proc;
  256.     procs[3] = ptran->gray->proc;
  257. }
  258.